﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;

namespace ETFK
{
    public class TestCase
    {
        public object PeopleAnswer { get; set; }
        public object ComputerAnswer { get; set; }
        public string MethodName { get; set; }


        public object[] Parameters = new object[0];

        public object PeopleResult { get; set; }
        public object ComputerResult { get; set; }

        public TestCase(object ComputerAnswer, object PeopleAnswer, string MethodName)
        {
            this.PeopleAnswer = PeopleAnswer;
            this.ComputerAnswer = ComputerAnswer;
            this.MethodName = MethodName;


            MethodInfo Method = PeopleAnswer.GetType().GetMethod(MethodName);

            int l = Method.GetParameters().Length;
            this.Parameters = new object[l];
        }

        public bool CheckMethod()
        {
            return CheckHelper.CheckMethodInfo(ComputerAnswer, PeopleAnswer, MethodName);
        }

        public void SetParameter(int Index, object Data)
        {
            Parameters[Index] = Data;

            ParameterInfo[] pis = ComputerAnswer.GetType().GetMethod(MethodName).GetParameters();

            CheckRecord.Add("".PadLeft(4) + "参数“" + pis[Index].Name + "”：");
            CheckRecord.Add("".PadLeft(8) + DataToString.GetString(Data));
        }

        public bool Run()
        {
            try
            {
                PeopleResult =  PeopleAnswer.GetType().GetMethod(MethodName).Invoke(PeopleAnswer, Parameters);
                CheckRecord.Add("您的运行结果：");
                CheckRecord.Add("".PadLeft(4) + DataToString.GetString(PeopleResult));
            }
            catch (Exception err)
            {
                CheckRecord.Add("您代码有错误！（" + err.Message + "）");
                return  false;
            }


            try
            {
                ComputerResult = ComputerAnswer.GetType().GetMethod(MethodName).Invoke(ComputerAnswer, Parameters);
                CheckRecord.Add("正确的运行结果：");
                CheckRecord.Add("    " + DataToString.GetString(ComputerResult));
            }
            catch (Exception err)
            {
                CheckRecord.Add("服务程序有错误！请联系管理员。（" + err.Message + "）");
                return false;
            }



            if (DataCompare.Compare(ComputerResult, PeopleResult) == true)
            {
                CheckRecord.Add(Color.SkyBlue, "在本次测试中，您代码的运行结果和正确答案完全一致！");
                return true;
            }
            else
            {
                CheckRecord.Add(Color.Red, "在本次测试中，您代码的运行结果和正确答案不一致！");
                return false;
            }
        }

    }
}
